/**
 * 
 */
package com.dhcc.android.base.common.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dhcc.android.base.R;
import com.dhcc.android.base.common.bean.File;
import com.dhcc.android.base.common.bean.ImageInfoBean;
import com.dhcc.android.base.common.bean.PhotosBean;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * 照片适配器
 * 
 * @author pengling
 * @date 2015-3-3
 */
public class ImageGridAdapter extends BaseAdapter {
	private Context mContext;
	public List<ImageInfoBean> imageInfoList;
	private File file;
	private ClickListenerInterface clickListenerInterface;
	public boolean deleteVisible = false;
	public Gallery gallery;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;
	public List<PhotosBean> photosList = new ArrayList<PhotosBean>();
	public void setClicklistener(ClickListenerInterface clickListenerInterface) {
		this.clickListenerInterface = clickListenerInterface;
	}

	public interface ClickListenerInterface {
		public void doDelete(int position);

		public void doBitmap(int position, View v);
	}

	public void setGallery(Gallery gallery) {
		this.gallery = gallery;
	}

	public ImageGridAdapter(Context context, List<ImageInfoBean> imageInfoList,
							File file) {
		mContext = context;
		this.imageInfoList = imageInfoList;
		this.file = file;
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(context));
		if(file!=null&&file.getPhoto()!=null&&file.getPhoto().getMaxsize().equals("1")){
			options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.mipmap.photo_no)
			.showImageForEmptyUri(R.mipmap.photo_no)
			.showImageOnFail(R.mipmap.photo_no).resetViewBeforeLoading(true).cacheInMemory(false)
			.cacheOnDisk(false).considerExifParams(false)
			.bitmapConfig(Bitmap.Config.RGB_565).build();
		}else{
			options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.mipmap.photo_no)
			.showImageForEmptyUri(R.mipmap.photo_no)
			.showImageOnFail(R.mipmap.photo_no).cacheInMemory(true)
			.cacheOnDisk(true).considerExifParams(true)
			.bitmapConfig(Bitmap.Config.RGB_565).build();
		}
	}
	
	public void changeData(List<ImageInfoBean> imageInfoList){
		this.imageInfoList = imageInfoList;
		this.notifyDataSetChanged();
	}

	public void changeDeleteVisible(boolean deleteVisible) {
		this.deleteVisible = deleteVisible;
	}

	public int getCount() {
		return imageInfoList.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.util_grid_photo_item, null);
			holder.text_title = (TextView) convertView
					.findViewById(R.id.text_title);
			holder.iv = (ImageView) convertView.findViewById(R.id.imageView);
			holder.delete = (ImageView) convertView.findViewById(R.id.delete);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		ImageInfoBean imageInfoBean = imageInfoList.get(position);
		
		System.out.println("####:"+imageInfoBean.getAbsolutePath());
		if (StringUtil.isEmpty(imageInfoBean.getAbsolutePath())) {
			holder.iv.setImageResource(R.mipmap.photo_no);
		} else {
			imageLoader.displayImage("file://"+imageInfoBean.getAbsolutePath()
					, holder.iv, options);
//			holder.iv.setImageBitmap(imageInfoBean.getBitmap());
		}
		int width = Constants.screenWidth;
		if (file == null) {
			holder.iv
					.setLayoutParams(new RelativeLayout.LayoutParams(240, 240));
			holder.text_title.setVisibility(View.GONE);
		} else {
			if (file.getPhoto().getMaxsize().equals("1")) {
				holder.iv.setLayoutParams(new RelativeLayout.LayoutParams(
						width * 9 / 10, width * 9 / 10));
			} else {
				holder.iv.setLayoutParams(new RelativeLayout.LayoutParams(
						width * 3 / 5, width * 3 / 5));
			}
			String msg = imageInfoBean.getImageMsg();
			if (StringUtil.isEmpty(msg)) {
				msg = "暂无";
			}
			holder.text_title.setText(msg);
		}

		if (deleteVisible) {
			holder.delete.setVisibility(View.VISIBLE);
		} else {
			holder.delete.setVisibility(View.GONE);
		}

		if(gallery!=null){
			holder.iv.setOnTouchListener(touchListener);//增加触摸事件，防止click与gallery事件冲突
			holder.delete.setOnTouchListener(touchListener);
		}
		
		// 点击某张图片
		holder.iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (clickListenerInterface != null) {
					clickListenerInterface.doBitmap(position, arg0);
				}
			}
		});
		// 删除某张
		holder.delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (clickListenerInterface != null) {
					clickListenerInterface.doDelete(position);
				}
			}
		});
		return convertView;
	}

	OnTouchListener touchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			gallery.onTouchEvent(event);
			return false;
		}
	};

	public void releaseBitmap(){
		if(imageInfoList.size()>0){
			for (ImageInfoBean imageInfoBean : imageInfoList) {
				if(imageInfoBean.getBitmap()!=null){
					imageInfoBean.getBitmap().recycle();
				}
			}
		}
		
	}
	public final class ViewHolder {
		public TextView text_title;
		public ImageView iv;
		public ImageView delete;
	}
}
